﻿
Partial Class WebForm_ManageVolunteer_SearchVOrganization
    Inherits MyUserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
     
            Me.BindDDLOrganizationID()
            RaiseEvent Loaded(Me, New EventArgs)

        End If

        If CanAdmin = False Then
            Me.gv.Columns(5).Visible = False
            Me.gv.Columns(6).Visible = False
        End If


    End Sub


    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        ObjectDataSource1.SelectParameters("VOrganizationID").DefaultValue = Me.ddlVOrganizationID.SelectedValue
        ObjectDataSource1.SelectParameters("DirectUOrganizationID").DefaultValue = Me.ddlOrganizationID.SelectedValue
        gv.DataBind()
    End Sub

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

        If e.CommandName = "Page" Then
            Return
        End If
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
        Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)


        If e.CommandName = "ReleaseAll" Then


            Using scope As New Transactions.TransactionScope

                sql.Length = 0
                sql.AppendFormat("Select VolunteerID from  VOrganizationMapping ")
                sql.AppendFormat("WHERE VOrganizationID='{0}'; ", key.Values(0))
                ReleaseAllData = dat.GetData(sql)

                sql.Length = 0
                sql.AppendFormat("DELETE VOrganizationMapping ")
                sql.AppendFormat("WHERE VOrganizationID='{0}'; ", key.Values(0))
                dat.AccData(sql)

                sql.Length = 0
                sql.AppendFormat("delete OtherOrganizationMapping where Source in ( 'VolunteerLeader', 'ViceVolunteerLeader', 'Secretary' ) ")
                sql.AppendFormat("and UOrganizationID = '-1,{0}' ", key.Values(0))
                dat.AccData(sql)
                scope.Complete()
            End Using
            ReleaseAllVOrganizationID = key.Values(0).ToString()
            Me.ChangeRecordControl1.InitAndShow()

        ElseIf e.CommandName = "Lock" Then
            sql.Length = 0
            sql.AppendFormat("Update VOrganization Set Locked='Y' ")
            sql.AppendFormat("where VOrganizationID ='{0}' ", key.Values(0))
            dat.AccData(sql)
        ElseIf e.CommandName = "UnLock" Then
            sql.Length = 0
            sql.AppendFormat("Update VOrganization Set Locked='N' ")
            sql.AppendFormat("where VOrganizationID ='{0}' ", key.Values(0))
            dat.AccData(sql)
        End If
        ObjectDataSource1.SelectParameters("VOrganizationID").DefaultValue = Me.ddlVOrganizationID.SelectedValue

        gv.DataBind()
    End Sub

    Public Event SelectedIndexChanged(ByVal source As Object, ByVal e As EventArgs)

    Protected Sub gv_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.SelectedIndexChanged
        RaiseEvent SelectedIndexChanged(Me, New EventArgs)
    End Sub

    Protected Sub ddlOrganizationID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrganizationID.SelectedIndexChanged
        If Me.ddlOrganizationID.SelectedValue <> "" Then
            Me.BindDDLVOrganizationID()
        Else
            Me.phVOrganizationID.Visible = False
            ddlVOrganizationID.Items.Clear()
        End If
        Me.ObjectDataSource1.SelectParameters("DirectUOrganizationID").DefaultValue = Me.ddlOrganizationID.SelectedValue

        Me.gv.DataBind()
    End Sub

    Protected Sub ChangeRecordControl1_OK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChangeRecordControl1.OK_Click
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        For Each dr As Data.DataRow In Me.ReleaseAllData.Rows
            sql.AppendFormat("Insert Into ChangeRecord (ChangeID,VOrganizationID,ChangeType,ChangeReason,CreateDate,CreateBy) ")
            sql.AppendFormat("Select '{0}' ", dr("VolunteerID").ToString())
            sql.AppendFormat(",'{0}' ", Me.ReleaseAllVOrganizationID)
            sql.AppendFormat(",'{0}' ", "QuitVOrganizaiton")
            sql.AppendFormat(",'{0}' ", Me.ChangeRecordControl1.Reason)
            sql.AppendFormat(",getdate() ")
            sql.AppendFormat(",'{0}' ;", clsAuth.GetUserID())
        Next

        dat.AccData(sql)
        MyObj.ShowMessage("儲存成功!")
    End Sub
End Class

Partial Class WebForm_ManageVolunteer_SearchVOrganization

 
    Private Sub BindDDLOrganizationID()


        Me.ddlOrganizationID.DataTextField = "OrganizationName"
        Me.ddlOrganizationID.DataValueField = "UOrganizationID"
        Me.ddlOrganizationID.DataSource = MyObj.GetOrganizationData(IIf(Me.CanAdmin = False And Me.CanQuery = True, True, False))
        Me.ddlOrganizationID.DataBind()

        If Me.ddlOrganizationID.Items.Count > 1 Then
            Me.ddlOrganizationID.Items.Insert(0, MyObj.GetDDLSelectAllItem())
        Else
            Me.BindDDLVOrganizationID()
        End If

    End Sub

    Private Sub BindDDLVOrganizationID()


        Me.ddlVOrganizationID.DataTextField = "VOrganizationName"
        Me.ddlVOrganizationID.DataValueField = "VOrganizationID"
        Me.ddlVOrganizationID.DataSource = MyObj.GetVOrganizationData(Me.ddlOrganizationID.SelectedValue)
        Me.ddlVOrganizationID.DataBind()

        If Me.ddlVOrganizationID.Items.Count > 1 Then
            Me.ddlVOrganizationID.Items.Insert(0, MyObj.GetDDLSelectAllItem())
        End If

        '初始值為隱藏
        Me.phVOrganizationID.Visible = True
    End Sub

    Public ReadOnly Property VOrganizationID() As String
        Get
            Return gv.SelectedValue
        End Get
    End Property
    Public Property ReleaseAllData() As Data.DataTable
        Get
            Return ViewState("ReleaseAllData")
        End Get
        Set(ByVal value As Data.DataTable)
            ViewState("ReleaseAllData") = value
        End Set
    End Property
    Public Property ReleaseAllVOrganizationID() As String
        Get
            Return ViewState("ReleaseAllVOrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("ReleaseAllVOrganizationID") = value
        End Set
    End Property
    Public Event Loaded(ByVal source As Object, ByVal e As EventArgs)

End Class